package com.neudu.yyzx_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yyzx_service.common.JSONReturn;
import com.neudu.yyzx_service.pojo.Nursecontent;
import com.neudu.yyzx_service.service.NursecontentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class NursecontentController {
    @Autowired
    private NursecontentService nursecontentService;
    @Autowired
    private JSONReturn jsonReturn;

    @RequestMapping("get_all_nursecontents")
    public String getAll(@RequestBody PageInfo pageInfo){
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            //可以根据数据库设置一个state字段值，匹配在住的客户查询
            List<Nursecontent> list = nursecontentService.list();
            PageInfo<Nursecontent> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_nursecontents_by")
    public String getByCondition(@RequestBody Nursecontent nursecontent){
        try {
            LambdaQueryWrapper<Nursecontent> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!ObjectUtils.isEmpty(nursecontent.getId()), Nursecontent::getId, nursecontent.getId())
                    .eq(!ObjectUtils.isEmpty(nursecontent.getSerialNumber()), Nursecontent::getSerialNumber, nursecontent.getSerialNumber())
                    .like(!ObjectUtils.isEmpty(nursecontent.getNursingName()), Nursecontent::getNursingName, nursecontent.getNursingName())
                    .eq(!ObjectUtils.isEmpty(nursecontent.getServicePrice()), Nursecontent::getServicePrice, nursecontent.getServicePrice())
                    .eq(!ObjectUtils.isEmpty(nursecontent.getMessage()), Nursecontent::getMessage, nursecontent.getMessage())
                    .eq(!ObjectUtils.isEmpty(nursecontent.getStatus()), Nursecontent::getStatus, nursecontent.getStatus())
                    .eq(!ObjectUtils.isEmpty(nursecontent.getExecutionCycle()), Nursecontent::getExecutionCycle, nursecontent.getExecutionCycle())
                    .eq(!ObjectUtils.isEmpty(nursecontent.getExecutionTimes()), Nursecontent::getExecutionTimes, nursecontent.getExecutionTimes())
                    .eq(!ObjectUtils.isEmpty(nursecontent.getIsDeleted()), Nursecontent::getIsDeleted, nursecontent.getIsDeleted());
            List<Nursecontent> nursecontents = nursecontentService.list(queryWrapper);
            return jsonReturn.returnSuccess(nursecontents);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("add_one_nursecontent")
    public String addOne(@RequestBody Nursecontent nursecontent){
        try {
            nursecontentService.save(nursecontent);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getnursecontentby_id")
    public String getById(@RequestParam("nursecontentid") Integer nursecontentid){
        try {
            Nursecontent nursecontent = nursecontentService.getById(nursecontentid);
            return jsonReturn.returnSuccess(nursecontent);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifynursecontentby_id")
    public String modifyById(@RequestBody Nursecontent nursecontent){
        try {
            nursecontentService.updateById(nursecontent);
            return jsonReturn.returnSuccess(nursecontent);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_all_nursingname")
    public String getAllName(){
        try {
            List<Nursecontent> list = nursecontentService.get_all_name();
            return jsonReturn.returnSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removenursecontent_by_id")
    public String removeByid(@RequestParam("nursecontentid") Integer nursecontentid){
        try {
            nursecontentService.removeById(nursecontentid);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getnursingname_by_levelname")
    public String getById(@RequestParam("levelname") String levelname){
        try {
            List<Nursecontent> list = nursecontentService.getnursingname_by_levelname(levelname);
            System.out.println(list);
            return jsonReturn.returnSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("get_nursecontents_level_by")
    public String getByConditionLevel(@RequestBody Map<String, Object> params){
        try {
            String nursingname = (String) params.get("nursingname");
            Integer nurselevelid = (Integer) params.get("nurselevelid");
            List<Nursecontent> nursecontents = nursecontentService.get_nursecontents_level_by(nursingname,nurselevelid);
            return jsonReturn.returnSuccess(nursecontents);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("update_status_content")
    public String updateStatus(@RequestBody Nursecontent nursecontent){
        try {
            if(nursecontent.getStatus()==1){
                nursecontent.setStatus(2);
            }else if (nursecontent.getStatus()==2){
                nursecontent.setStatus(1);
            }
            nursecontentService.updatestatus(nursecontent);
            return jsonReturn.returnSuccess(nursecontent);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}
